﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PCMSofCC;

namespace PCMSofCC.BLL
{
    public static class Records
    {
        public static void AddRecord(DateTime _date, Guid _userId, int _siteId, int _chartId)
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            var newRecord = new Record
            {
                Date = _date.Date,
                UserId = _userId,
                SiteId = _siteId,
                ChartId = _chartId
            };
            db.Record.InsertOnSubmit(newRecord);

            var entries =
                from belong in db.EntryBelongToChart
                where belong.ChartId == _chartId
                select belong.ScoreEntry;
            foreach (var entry in entries)
            {
                var newRecordEntry = new RecordEntry
                {
                    Record = newRecord,
                    ScoreEntry = entry,
                };
                db.RecordEntry.InsertOnSubmit(newRecordEntry);
            }
            db.SubmitChanges();
        }

        public static bool DoesBelongTo(int _recordId, string _userName)
        {
            return GetRecord(_recordId).aspnet_Users.UserName == _userName;
        }

        public static System.Data.Linq.ISingleResult<GetCompleteRecordByChartId_个结果> GetCompleteRecord(int _chartId)
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            return db.GetCompleteRecordByChartId(_chartId);
        }

        public static Record GetRecord(int _recordId)
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            var q =
                from r in db.Record
                where r.RecordId == _recordId
                select r;
            return q.Single();
        }

        public static Record GetRecord(int _siteId, DateTime _date)
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            var q =
                from r in db.Record
                where r.SiteId == _siteId && r.Date == _date
                select r;
            return q.Single();
        }

        public static IQueryable<Record> GetRecords(DateTime _date)
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            var q =
                from r in db.Record
                where r.Date == _date
                select r;
            return q;
        }
    }
}
